package com.weitu.securitycheck.service.impl;

import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.securitycheck.entity.CheckPlan;
import com.weitu.securitycheck.entity.CheckPlanCriteria;
import com.weitu.securitycheck.mapper.CheckPlanMapper;
import com.weitu.securitycheck.service.CheckPlanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckPlanServiceImpl implements CheckPlanService {
    private final CheckPlanMapper checkPlanMapper;

    private static final Logger logger = LoggerFactory.getLogger(CheckPlanServiceImpl.class);

    @Autowired
    public CheckPlanServiceImpl(CheckPlanMapper checkPlanMapper) {
        this.checkPlanMapper = checkPlanMapper;
    }

    @Override
    public int countByCriteria(CheckPlanCriteria example) {
        int count = this.checkPlanMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public CheckPlan selectByPrimaryKey(String id) {
        return this.checkPlanMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckPlan> selectByCriteria(CheckPlanCriteria example) {
        return this.checkPlanMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.checkPlanMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(CheckPlan record) {
        return this.checkPlanMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(CheckPlan record, CheckPlanCriteria example) {
        return this.checkPlanMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(CheckPlan record) {
        return this.checkPlanMapper.insertSelective(record);
    }

    @Override
    @Transactional
    public boolean saveAccessoryAndInsert(CheckPlan checkPlan) {
        if (AccessoryUtil.saveAccerssoryInfo(checkPlan.getId())) {
            if (checkPlanMapper.insertSelective(checkPlan) > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional
    public boolean saveAccessoryAndUpdate(CheckPlan checkPlan) {
        if (AccessoryUtil.saveAccerssoryInfo(checkPlan.getId())) {
            if (checkPlanMapper.updateByPrimaryKeySelective(checkPlan) > 0) {
                return true;
            }
        }
        return false;
    }
}